﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.View;
using ERP.Reports;
using ERP.Hoadon;

namespace ERP.Hoadon
{
    public partial class viewERP_Congno : DevExpress.XtraEditors.XtraForm
    {
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public int flag = 0;
        public int count = 0;
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();

        public viewERP_Congno()
        {
            InitializeComponent();
        }

        private void viewERP_Congno_Load(object sender, EventArgs e)
        {
            this.Load_Data();
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnDatcoc.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnDatcoc.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                    this.btnIn.Enabled = false;
                }
            }
        }
        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            ds = this.hoadon.Get_List();
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void btnChitietHD_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0)
                {
                    viewERP_NhapHoadon nhd = new viewERP_NhapHoadon();
                    nhd.user = this.user;
                    nhd.flag = 3;
                    nhd.hoadon = this.hoadon;
                    nhd.MdiParent = this.MdiParent;
                    nhd.Show();
                }
            }
        }

        private void btnIn_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.inHoadon();
        }

        private void btnDatcoc_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0 && this.hoadon.conno > 0)
                {
                    viewERP_Hoadon_Thanhtoan gv = new viewERP_Hoadon_Thanhtoan();
                    gv.user = this.user;
                    //gv.kh = kh;
                    gv.hoadon = this.hoadon;
                    gv.ShowDialog();
                    this.Load_Data();
                }
                else
                {
                    XtraMessageBox.Show("Hóa đơn này đã thanh toán hết tiền nợ!", "Thông báo");
                }
            }
        }

        private void navBarItem1_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() > 0)
                {
                    viewERP_LichsuThanhtoan gv = new viewERP_LichsuThanhtoan();
                    gv.user = this.user;
                    //gv.kh = kh;
                    gv.hoadon = this.hoadon;
                    gv.ShowDialog();
                    this.Load_Data();
                }
                //else
                //{
                //    XtraMessageBox.Show("Hóa đơn này đã thanh toán hết tiền nợ!", "Thông báo");
                //}
            }
        }

        private void inHoadon()
        {
            if (this.count > 0 && this.gridView1.FocusedRowHandle >= 0 && this.gridView1.FocusedRowHandle <= this.count)
            {
                this.hoadon.id_hd = Convert.ToInt32(this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, this.grdcID));
                if (this.hoadon.Get_Info() <= 0)
                {
                    return;
                }

                cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
                kh.id_kh = this.hoadon.id_kh;
                kh.Get_Info();
                DataSet ds = new DataSet("ds");
                string ban1 = kh.tenkhachhang + ", Ngày: " + DateTime.Now.ToShortDateString();
                cs_erp_chitiet_hoadonInfo ct = new cs_erp_chitiet_hoadonInfo();
                ct.id_hoadon = this.hoadon.id_hd;
                ds = ct.Get_List();
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("Không có số liệu...", "Thông báo!");
                    return;
                }
                cs_hmr_donviInfo dv = new cs_hmr_donviInfo();
                dv.captren = "0";
                dv.Get_Info_by_Parents();
                rptDoanhthu rpt = new rptDoanhthu();
                rpt.DataSource = ds.Tables[0];
                rpt.ds_BaoCao = ds.Tables[0];
                rpt.dsHoadon = this.hoadon.Get_HD_Tinhtien().Tables[0];
                rpt.ban = ban1;
                rpt.donvi = dv.tendonvi;
                rpt.diachi = dv.diachi + " - " + dv.sodt_fax;
                rpt.In();
                rpt.ShowPreview();
                //rpt.e

                //rpt.CreateDocument(true);
                //viewReport frm = new viewReport();
                ////rpt.PrintingSystem = frm.printingSystem1;
                //frm.dt_In = ds.Tables[0];
                //frm.ban = ban1;
                //frm.int_BaoCao = 1;
                //frm.rpt = rpt;

                ////frm.MdiParent = this.MdiParent;
                //frm.ShowDialog();
            }
        }

        private void btnThoat_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.Dispose(true);
        }
    }
}